package com.etpjava.dao.impl;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import com.etpjava.dao.InviteJobDao;
import com.etpjava.model.InviteJob;
import com.etpjava.util.Page;
public class InviteJobImpl implements InviteJobDao {
	static Log logger = LogFactory.getLog(InviteJobImpl.class);
	public Session getSession() {
		Configuration cfg = new Configuration();
		SessionFactory sessionFactory = cfg.configure().buildSessionFactory();
		return sessionFactory.openSession();
	}
	@Override
	public void save(InviteJob invite) {
		// TODO Auto-generated method stub
       Session session=getSession();
       session.beginTransaction();
       session.save(invite);
       session.getTransaction().commit();
       session.close();
	}
    @Override
	public void update(InviteJob invite) {
		  Session session=getSession();
		  session.beginTransaction();
	      session.update(invite);
	      session.getTransaction().commit();
 	}
    @Override
	public void delete(InviteJob invite) {
		// TODO Auto-generated method stub
		  Session session=getSession();
		  session.beginTransaction();
	       session.delete(invite);
	       session.getTransaction().commit();
	       session.close();
	}

	@Override
	public InviteJob findById(int id) {
		// TODO Auto-generated method stub
		  Session session=getSession();
		  session.beginTransaction();
		  InviteJob invite=(InviteJob) session.get("com.etpjava.model.InviteJob", new Integer(id));
		  session.getTransaction().commit();
	      session.close();
	      return invite;
	}

	@Override
	public List<InviteJob> findAllInvite() {
		// TODO Auto-generated method stub
		Session session=getSession();
		Query query=session.createQuery("from InviteJob");
		@SuppressWarnings("unchecked")
		List<InviteJob> list=query.list();
		session.close();
		return list;
	}

	@Override
	public List<InviteJob> findInviteByName(String name) {
		// TODO Auto-generated method stub
		Session session=getSession();
		String hql="from InviteJob n where n.name like ?";
		@SuppressWarnings("unchecked")
		List<InviteJob> list=session.createQuery(hql).setString(0, "%"+name+"%").list();
		session.close();
		return list;
	}

	@Override
	public List<?> queryForPage(int offset, int length) {
		// TODO Auto-generated method stub
		
	       Session session=getSession();
	       String hql="from InviteJob";
	       Query query=session.createQuery(hql);
	       query.setFirstResult(offset);
	       query.setMaxResults(length);
	       List<?> list=query.list();
	      
	       session.close();
		   return list;
		}
  

    @Override
	public List<InviteJob> findByIds(int id) {
		// TODO Auto-generated method stub
		Session session=getSession();
	    Query query=session.createQuery("from InviteJob t where t.id=?").setInteger(0, id);
	    @SuppressWarnings("unchecked")
		List<InviteJob> list=query.list();
	    session.close();
	    return list;
	}



	@Override
	public void delete(Integer id) {
		// TODO Auto-generated method stub
		
	}







@Override
public void queryForPage(Page pagelist) {
	// TODO Auto-generated method stub
	Session session=getSession();
 	int rowcount=session.createQuery("from InviteJob").list().size();
    logger.info(rowcount);
    System.out.println("��"+rowcount);
	pagelist.setRowTotalCount(rowcount);
	logger.info("������:"+pagelist.getRowTotalCount());
	logger.info("��ҳ��:"+pagelist.getPageTotalCount());
	Query query=session.createQuery("from InviteJob")
	.setFirstResult(pagelist.getRowStartCount())
	.setMaxResults(Page.pagesize);
    List list=query.list();
	pagelist.setList(list);
	logger.info(list.get(0));
	logger.info(rowcount);
	logger.info(pagelist.getRowStartCount());
	logger.info(pagelist.getList().size());
	logger.info(pagelist.getPageNum());
	
	session.close();
  }
@Override
public void findProfessionForPage(Page pagelist, String profession) {
	// TODO Auto-generated method stub
	Session session=getSession();
	String hql="from InviteJob t where t.profession like ?";
	int rowcount=session.createQuery(hql).setString(0, "%"+profession+"%").list().size();
	logger.info(rowcount);
	pagelist.setRowTotalCount(rowcount);
	Query query=session.createQuery(hql).setString(0, "%"+profession+"%");
	query.setFirstResult(pagelist.getRowStartCount());
	query.setMaxResults(Page.pagesize);
	List list=query.list();
	pagelist.setList(list);
	session.close();
	
}
@Override
public void findByAgeForPage(Page pagelist, String birth) {
	// TODO Auto-generated method stub
	Session session=getSession();
	String hql="from InviteJob t where t.birthday like ?";
	int rowcount=session.createQuery(hql).setString(0, "%"+birth+"%").list().size();
	pagelist.setRowTotalCount(rowcount);
	List list=session.createQuery(hql).setString(0, "%"+birth+"%").setFirstResult(pagelist.getRowStartCount())
	.setMaxResults(Page.pagesize).list();
	pagelist.setList(list);
	session.close();	
}        
@Override
public void findEducationForPage(Page pagelist, String education) {
	// TODO Auto-generated method stub
	Session session=getSession();
	String hql="from InviteJob t where t.education like ?";
	int rowcount=session.createQuery(hql).setString(0, "%"+education+"%").list().size();
	pagelist.setRowTotalCount(rowcount);
	List list=session.createQuery(hql).setString(0,"%"+education+"%").setFirstResult(pagelist.getRowStartCount())
	.setMaxResults(Page.pagesize).list();
	pagelist.setList(list);
	session.close();
 }
@Override
public void findExperienceForPage(Page pagelist, String experience) {
	// TODO Auto-generated method stub
	Session session=getSession();
	String hql="from InviteJob t where t.experience like ?";
	
	int rowcount=session.createQuery(hql).setString(0, "%"+experience+"%").list().size();
	logger.info(rowcount);
	pagelist.setRowTotalCount(rowcount);
	List list=session.createQuery(hql).setString(0, "%"+experience+"%").setFirstResult(pagelist.getRowStartCount())
	.setMaxResults(Page.pagesize).list();
	pagelist.setList(list);
	logger.info(pagelist.getPageTotalCount());
	logger.info(pagelist.getPageNums().length);
	session.close();
}
@Override
public void findFuzzyForPage(Page pagelist, Map<String, String> sw) {
	// TODO Auto-generated method stub
	Session session=getSession();
	session.beginTransaction();
	StringBuilder sb=new StringBuilder();
	if(!sw.get("name").isEmpty()){
		sb.append("name like '"+"%"+sw.get("name")+"%"+"'"+"and"+"\n");
		logger.info("�������:"+sw.get("name"));
	}
	if(sw.containsKey("birth")){
		sb.append("birthday like'"+"%"+sw.get("birth")+"%"+"'"+"and"+"\n");
	}
	if(!sw.get("profession").isEmpty()){
		sb.append("profession like'"+"%"+sw.get("profession")+"%"+"'"+"and"+"\n");
	}
	if(!sw.get("education").isEmpty()){
		sb.append("education like'"+"%"+sw.get("education")+"%"+"'"+"and");
	}
	String hql=null;
	if(sb.toString().indexOf("like")==-1){
		hql="from InviteJob";
	}else{
		int kk=sb.lastIndexOf("and");
		hql="from InviteJob t where "+sb.subSequence(0, kk);
	}
	logger.info(hql);
	int rowcount=session.createQuery(hql).list().size();
	pagelist.setRowTotalCount(rowcount);
	List list=session.createQuery(hql).setFirstResult(pagelist.getRowStartCount()).setMaxResults(Page.pagesize).list();
	pagelist.setList(list);
	session.close();
	
}
@Override
public void findByBirth(Page pagelist, String birth) {
	// TODO Auto-generated method stub
	Session session=getSession();
	String hql="from InviteJob t where t.birthday >='"+birth+"'";
	int rowcount=session.createQuery(hql).list().size();
	pagelist.setRowTotalCount(rowcount);
	List list=session.createQuery(hql).setFirstResult(pagelist.getRowStartCount()).setMaxResults(Page.pagesize).list();
	pagelist.setList(list);
	session.close();

}
@Override
public void findByEmployNoForPage(Page pagelist) {
	// TODO Auto-generated method stub
	Session session=getSession();
	String hql="from InviteJob t where t.isEmploied='��'";
	int rowcount=session.createQuery(hql).list().size();
	pagelist.setRowTotalCount(rowcount);
	List list=session.createQuery(hql).setFirstResult(pagelist.getRowStartCount()).setMaxResults(Page.pagesize).list();
	pagelist.setList(list);
	session.close();
}


}

